<!--
作者：fengzhu1131@126.com
时间：2015-12-07
描述：在此记录一下，下面接口获取数据后，打印一直都是乱码的问题，原因在于本文件的浏览格式，需要设置<meta charset="UTF-8">
-->
<?php
/**
 * @description 该页面为公众号跳转到其他页面，携带用户ID参数跳转,
 * 可以从地址中获取code，并通过code获取真实的用户ID号
 * 详细信息参见：http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
 */
header("Content-type: text/html; charset=utf-8");
require_once "common/jssdk.php";
$jssdk = new JSSDK();
//打印当前访问路径
//print_r('http://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]);
$code = $_GET["code"];
$url = "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER["SERVER_PORT"] . "/duoduo-policy/policylistwx.html";
//$url = "http://mlop.ngrok.natapp.cn/duoduo-policy/policylistwx.html";

if (!empty($code)) {
	$auth = $jssdk -> getAuthorizeAccessToken($code);
	//print_r($auth);
	$authJson = json_decode($auth);
	$openid = $authJson -> openid;
	$url = $url . "?openid=" . $openid;
	try {
		//获取用户信息并填写到数据库中
		$userinfo = $jssdk -> getUserInfoByOpenid($authJson -> access_token, $openid);

		$userinfoJSON = json_decode($userinfo);
		//遍历用户权限
		$privilege = "";
		foreach ($userinfoJSON -> privilege as $value) {
			$privilege = $privilege . $value . ",";
		}
		/*require_once "common/MySQLUtils.php";
		 $mysqlu = new MySQLUtils();
		 //已经存在就更新，否则就直接插入数据库
		 //查询数据是否存在
		 $u = $mysqlu -> query("select count(1) from t_user where openid='" . $openid . "'");
		 //print_r("rows:".$mysqlu->num_rows($u));
		 if ($u == "0") {//说明没有数据，就需要添加数据
		 $fields = array("openid" => $openid, "name" => $userinfoJSON -> nickname, "weixincode" => $userinfoJSON -> nickname, "sex" => $userinfoJSON -> sex, "headimgurl" => $userinfoJSON -> headimgurl, "country" => $userinfoJSON -> country, "province" => $userinfoJSON -> province, "city" => $userinfoJSON -> city, "privilege" => $privilege, "status" => "1", "createtime" => date("Y-m-d H:m:s"), "logintime" => date("Y-m-d H:m:s"));
		 $mysqlu -> insert("t_user", $fields);
		 } else {//说明数据存在，就更新一下数据
		 $fields = array("openid" => $openid, "name" => $userinfoJSON -> nickname, "weixincode" => $userinfoJSON -> nickname, "sex" => $userinfoJSON -> sex, "headimgurl" => $userinfoJSON -> headimgurl, "country" => $userinfoJSON -> country, "province" => $userinfoJSON -> province, "city" => $userinfoJSON -> city, "privilege" => $privilege, "status" => "1");
		 $mysqlu -> update("t_user", $fields, "openid='" . $openid . "'");
		 }*/
		$fields = "openid=" . $openid;
		$fields = $fields . "&name=" . $userinfoJSON -> nickname;
		$fields = $fields . "&weixincode=" . $userinfoJSON -> nickname;
		$fields = $fields . "&sex=" . $userinfoJSON -> sex;
		$fields = $fields . "&headimgurl=" . $userinfoJSON -> headimgurl;
		$fields = $fields . "&country=" . $userinfoJSON -> country;
		$fields = $fields . "&province=" . $userinfoJSON -> province;
		$fields = $fields . "&city=" . $userinfoJSON -> city;
		$fields = $fields . "&privilege=" . $privilege;
		$fields = $fields . "&status=1";
		require_once "common/Utils.php";

		//调用接口处理数据
		Utils::curl_post("http://pro.mdg-app.com:7001/thinkphpddwx/index.php/Home/DdWx/checkWeiXinUser",$fields);		
		//Utils::curl_post("http://mlop.ngrok.natapp.cn/thinkphpddwx/index.php/Home/DdWx/checkWeiXinUser", $fields);
	} catch(Exception $e) {

	}
}
Header("HTTP/1.1 303 See Other");
Header("Location: $url");
exit ;
?>